
public class Test {
    public static void main(String[] args) throws Exception {
        Handler h1 = new Manager();
        Handler h2 = new HR();
        Handler h3 = new Finance();
        h1.setNext(h2);
        h2.setNext(h3);

        h1.handle("涨薪"); // 经理→HR接住处理
        h1.handle("报销"); // 经理→HR→财务接住
        h1.handle("辞职"); // 无人处理
    }

}

// 责任链模式（Chain of Responsibility）= “踢皮球，直到有人接住”
// 把请求沿一条链传递，每个处理者要么自己解决，要么甩给下一个；客户端不知道谁会处理，只负责把请求丢给链头
abstract class Handler {
    private Handler next; // 后继节点

    void setNext(Handler next) {
        this.next = next;
    }

    // 模板：自己能处理就处理，否则传下去
    public void handle(String request) {
        if (canHandle(request)) {
            process(request);
        } else if (next != null) {
            next.handle(request);
        } else {
            System.out.println("无人处理：" + request);
        }
    }

    protected abstract boolean canHandle(String request);

    protected abstract void process(String request);
}

class Manager extends Handler {
    protected boolean canHandle(String r) {
        return r.equals("请假");
    }

    protected void process(String r) {
        System.out.println("经理批准请假");
    }
}

class HR extends Handler {
    protected boolean canHandle(String r) {
        return r.equals("涨薪");
    }

    protected void process(String r) {
        System.out.println("HR处理涨薪");
    }
}

class Finance extends Handler {
    protected boolean canHandle(String r) {
        return r.equals("报销");
    }

    protected void process(String r) {
        System.out.println("财务报销完毕");
    }
}